package com.bw.paper.mvp.presenter;

import android.Manifest;
import android.content.pm.PackageManager;

import androidx.core.app.ActivityCompat;

import com.bw.paper.App;
import com.bw.paper.mvp.base.model.BaseEntity;
import com.bw.paper.mvp.base.presenter.BasePresenter;
import com.bw.paper.mvp.contact.SplashContract;
import com.bw.paper.entity.ToKenEntity;
import com.bw.paper.utils.LogUtils;
import com.bw.paper.utils.SpUtils;

import java.util.HashMap;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;

public class    SplashPresenter extends BasePresenter<SplashContract.ISplashModel, SplashContract.ISplashView> {
    @Inject
    public SplashPresenter(SplashContract.ISplashModel mModel, SplashContract.ISplashView mView) {
        super(mModel, mView);
    }
    //请求服务器token方法
    public void requestToken() {
        HashMap<String, Object> map = new HashMap<>();
        if (ActivityCompat.checkSelfPermission(App.getInstance().getApplicationContext(),
                Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {

            return;
        }
//        String imie = PhoneUtils.getIMEI();
        map.put("imie",+System.currentTimeMillis());
        mModel.reqstToken(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::onNext);
    }

    @Override
    public void onNext(@NonNull BaseEntity baseEntity) {
      ToKenEntity toKenEntity= (ToKenEntity) baseEntity;
        //请求成功逻辑校验
        LogUtils.show("SplashPresenter->onNext");

        if (toKenEntity.status.equals("200")){
            //保存token到本地SP存储中
            SpUtils.saveData("token",toKenEntity.getValues().token);
            LogUtils.show("SplashPresenter->onNext1");
            //通知activity界面跳转
            mView.refresh();
        }
    }
}
